﻿using System;
using System.Linq;
using Org.Flowertwig.Media.Information.Services;
using Org.Flowertwig.Media.Services.InfoService;
using Raven.Client.Indexes;
using Org.Flowertwig.Media.Information;

namespace InformationService
{
    class Program
    {
        const string resetCommand = "/reset";

        static void Main(string[] args)
        {
            // If we have no arguments, lets start service
            if (args.Length == 0)
            {
                var service = new InfoCollector();
                service.Start();

                Console.Read();
                service.Stop();
            }
            else
            {
                foreach (var arg in args)
                {
                    if (arg.StartsWith(resetCommand, StringComparison.CurrentCultureIgnoreCase))
                    {
                        Reset();
                    }
                    else
                    {
                        Console.WriteLine("Unknown command: " + arg);
                    }
                }
            }
        }

        private static void Reset()
        {
            Console.WriteLine("# Reset - Creating Index");
            Storage.AddIndex("Series/DeleteIndexes",
                                      new IndexDefinitionBuilder<Serie>
                                      {
                                          Map =
                                              myClasses =>
                                              from myClass in myClasses
                                              select new
                                              {
                                                  Id = myClass.Id
                                              }
                                      });

            Console.WriteLine("# Reset - Removing items matching index");
            Storage.DeleteAllItemsOfType<Serie>("Series/DeleteIndexes");
            Console.WriteLine("# Reset - Items has been removed");
        }
    }
}
